package edu.fight.shiro.session;

import org.apache.shiro.web.servlet.ShiroHttpServletRequest;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.apache.shiro.web.util.WebUtils;
import org.springframework.util.StringUtils;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.Serializable;

/**
 * 自定义sessionmanager
 */
//public class MySessionManager extends DefaultWebSessionManager {
//
//    /**
//     * 指定sessionid的获取方式
//     * @param request
//     * @param response
//     * @return
//     */
//    @Override
//    protected Serializable getSessionId(ServletRequest request, ServletResponse response) {
//
//        String id  = WebUtils.toHttp(request).getHeader("Authorization");
//        if(StringUtils.isEmpty(id)){
//            // 若无，生成sessionId
//            return super.getSessionId(request, response);
//        }else{
//            // 返回sessionId
//            request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID_SOURCE, "header");
//            request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID, id);
//            //automatically mark it valid here.  If it is invalid, the
//            //onUnknownSession method below will be invoked and we'll remove the attribute at that time.
//            request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID_IS_VALID, Boolean.TRUE);
//            return id;
//        }
//
////        return super.getSessionId(request, response);
//    }
//}
